package com.wc.servlet; /**
 * @ClassName:${NAME}
 * @Author: 鹿建文
 * @Date: 2022/4/13  - 11:11
 * @Describe:
 */

import com.wc.dao.bean.PageBean;
import com.wc.domain.User;
import com.wc.service.UserService;
import com.wc.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        switch (op) {
            case "getPage":
                userPage(request, response);
                break;
            case "logout":
                logout(request, response);
                break;
            case "isTelExists":
                isTelExists(request, response);
            default:
                break;
        }
    }
    private void isTelExists(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tel = request.getParameter("tel");
        boolean telExists =false;
        try {
          telExists = userService.isTelExists(tel);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String res=telExists?"存在":"不存在";
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.write(res);
        out.flush();
        out.close();
    }
        private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将Session中的admin信息删除
        HttpSession session = request.getSession();
        session.setAttribute("admin", null);
        //打开首页
        request.getRequestDispatcher("index.ftl").forward(request, response);
    }
    private void userPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageNumStr = request.getParameter("pageNum");
        if (pageNumStr == null) {
            pageNumStr = "1";
        }
        Integer pageNum = Integer.valueOf(pageNumStr);

        String pageSizeStr = request.getParameter("pageSize");
        if (pageSizeStr == null) {
            pageSizeStr = "10";
        }
        Integer pageSize = Integer.valueOf(pageSizeStr);

        //根据前端传递过来的页面大小及数据库中数据总条数，确定最多有多少页
        int count = 0;
        try {
            count = userService.getCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int m = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        pageNum = Math.min(m, pageNum);

        PageBean<User> userPageBean = null;
        try {
            userPageBean = userService.getPage(pageNum, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        request.setAttribute("userPageBean", userPageBean);
        request.setAttribute("pageSize", pageSize);

        //成功到成功页面
        request.getRequestDispatcher("index.ftl").forward(request, response);
    }

        @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        switch (op) {
            case "addUser":
                addUser(request, response);
                break;
            case "":
                break;
            default:
                break;
        }
    }
    private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("name");
        String genderStr = request.getParameter("gender");
        boolean gender = false;//nv
        if (genderStr != null && genderStr.equals("1")) {
            gender = true;
        }
        String tel = request.getParameter("tel");
        String creditsStr = request.getParameter("credits");
        Integer credits = Integer.valueOf(creditsStr);
        User user = new User(username, gender, tel, credits);
        System.out.println(user);
        boolean add = false;
        try {
            add = userService.add(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //成功，到用户列表页面
        if (add) {
            System.out.println("success");
            //         response.sendRedirect("userPageServlet");
            response.setHeader("refresh", "0;url=user?op=getPage");
        } else {
            System.out.println("whathappen");
            //失败，经过错误提示页面后，再次打开添加页面
            request.setAttribute("msg", "注册失败，请在3s后重新登录！");
            request.setAttribute("aim", "user_add.html");
            request.getRequestDispatcher("error.ftl").forward(request, response);
        }
    }
}
